package com.gxar.quick.ar.server.base.controller;

import com.gxar.common.entity.Result;
import com.gxar.common.entity.login.LoginUserThread;
import com.gxar.common.entity.login.UserProfile;
import com.gxar.quick.ar.server.base.facade.ArProtocolConvertFacade;
import com.gxar.quick.ar.server.base.model.protocol.CommonInputWorkInfo;
import com.gxar.quick.ar.server.base.model.protocol.vo.request.QueryFavoritesWorkRequest;
import com.gxar.quick.ar.server.base.model.works.vo.response.WorksInfoResponse;
import com.gxar.quick.ar.server.db.service.FavoritesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Objects;

/**
 * @author Linsy
 * @version 1.0
 * @created 2023/12/06
 */
@Api(tags = "收藏接口")
@RestController
@RequestMapping("/api/favorites")
@RequiredArgsConstructor
@Slf4j
public class FavoritesController {

    private final FavoritesService favoritesService;

    private final ArProtocolConvertFacade arProtocolConvertFacade;

    @ApiOperation("查看作品是否收藏")
    @ApiImplicitParam(name = "id", value = "作品id", dataType = "long", required = true, paramType = "path")
    @GetMapping("/works/{id}")
    public Result<WorksInfoResponse> getWorksFavorites(@PathVariable(name = "id") Long id) {
        UserProfile userProfile = LoginUserThread.get();
        return favoritesService.getWorksFavorites(userProfile, id);
    }

    @ApiOperation("查看作品是否收藏 - 经协议转换")
    @PostMapping("/v2/works")
    public Result<WorksInfoResponse> getWorksFavoritesV2(@RequestBody @Valid QueryFavoritesWorkRequest request) {
        UserProfile userProfile = LoginUserThread.get();
        Result<WorksInfoResponse> response = favoritesService.getWorksFavorites(userProfile, request.getId());
        if (request.isNeedConvertProtocol() && Objects.nonNull(response.getData())) {
            CommonInputWorkInfo commonWorkInfo = CommonInputWorkInfo.from(response.getData());
            arProtocolConvertFacade.convertProtocolConfiguration(commonWorkInfo, request);
            response.getData().setIdentify(commonWorkInfo.getIdentify());
            response.getData().setContent(commonWorkInfo.getContent());
        }
        return response;
    }

}
